package com.open.party.cloud.view.examine.examineForGanbu

import android.os.Bundle
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.ExamineClassEntity
import com.open.party.cloud.model.ExamineClassJoinVO
import com.open.party.cloud.model.ExamineGanBuRegisterVO
import com.open.party.cloud.model.ExamineSignBean
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.examine_ganbu_register_activity.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ExamineGanBuRegisterActivity : AppEventBusTitleBaseActivity() {

//    提交干部考试人员信息
//    schoolUser/insertExamUser      post
//    参数 examinationId（考试id），userType（职务），userName（姓名），unitName（单位），userPhone（电话）

    var viewModel: ExamineViewModel? = null
    private var examinationId: String? = null
    var vo: ExamineGanBuRegisterVO? = null

    override fun getLayoutResId(): Int = R.layout.examine_ganbu_register_activity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("考试人员信息")
        examinationId = intent.getStringExtra("examinationId") as String
        initView()

        viewModel = ExamineViewModel()

        submitBtn.setOnClickListener {
            submit()
        }
    }

    private fun initView() {
        vo = ExamineGanBuRegisterVO()
        vo!!.examinationId = examinationId

        val user = LocalCache.getLoginUser()
        userNameEt.setText(XUtils.string().getNotNullValue(user.name))
        userPhoneEt.setText(XUtils.string().getNotNullValue(user.mainmobile))
        unitNameEt.setText("")
    }

    private fun submit() {
        val userName = userNameEt.text.toString()
        if (XUtils.string().isEmpty(userName)) {
            XUtils.toast().show("请输入姓名")
            return
        }
        vo!!.userName = userName

        val phoneNum = userPhoneEt.text.toString()
        if (XUtils.string().isEmpty(phoneNum)) {
            XUtils.toast().show("请输入电话号码")
            return
        }
        if (phoneNum.length != 11 || '1' != phoneNum[0]) {
            XUtils.toast().show("电话号码可能不正确")
            return
        }
        vo!!.userPhone = phoneNum

        val unitName = unitNameEt.text.toString()
        if (XUtils.string().isEmpty(unitName)) {
            XUtils.toast().show("请输入单位")
            return
        }
        vo!!.unitName = unitName

        //
        val userType = userTypeEt.text.toString()
        if (XUtils.string().isEmpty(userType)) {
            XUtils.toast().show("请输入职务")
            return
        }
        vo!!.userType = userType

        showLoadingDialog("正在提交")
        viewModel!!.insertExamUser(vo!!)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<String>) {
        if (result.eventType != "insertExamUser") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                XUtils.toast().show("加入成功")
                finish()
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }
}